import Taro, { Component } from '@tarojs/taro'
import { View, Text, Radio, RadioGroup, Input } from '@tarojs/components'
import './publish.scss'
import { post } from '../../utils/http'
import Feedback from '../../components/feedback/feedback'

export default class publish extends Component {

  state = {
    address: "点击选择，要勾选哦~",
    latitude: "",
    longitude: "",
    type: "buy",
    message: "",
    contact: "",
    issubmit: false,
    issuccess: null
  }

  chooseLocation() {
    Taro.chooseLocation({
      complete: (res) => {
        console.log(res);
        this.setState({
          longitude: res.longitude,
          latitude: res.latitude,
          address: res.address
        })
      },
    })
  }
  //接口参数：address，latitude，longitude，message，contact，type（sell，buy），openid（可不传）

  typechange(event) {
    console.log(event)
    this.setState({
      type: event.detail.value
    })
  }

  messageinput(event) {
    this.setState({
      message: event.detail.value
    })
  }
  contactinput(event) {
    this.setState({
      contact: event.detail.value
    })
  }

  async handleSubmit() {

    let options = {
      mask: true,
      duration: 2000,
      icon: "none"
    }

    if (this.state.address === "点击选择，要勾选哦~") {
      Taro.showToast({
        title: '地址不能为空',
        ...options
      })
      return;
    }

    if (!this.state.message) {
      Taro.showToast({
        title: '说明不能为空',
        ...options
      })
      return;
    }

    if (!this.state.contact) {
      Taro.showToast({
        title: '联系方式不能为空',
        ...options
      })
      return;
    }

    let params = {
      address: this.state.address,
      latitude: this.state.latitude,
      longitude: this.state.longitude,
      message: this.state.message,
      contact: this.state.contact,
      type: this.state.type
    }
    Taro.showLoading({
      title: '',
    })
    let rs = await post('index.php/trade/add_item', params)
    Taro.hideLoading()
    this.setState({
      issubmit: true,
      issuccess: rs.ret
    })
    console.log(rs);
  }

  goHomePage() {
    console.log(0)
    Taro.navigateTo({
      url: '/pages/index/index',
    })
  }

  republish() {
    console.log(1)
    this.setState({
      issuccess: null,
      issubmit: false
    })
  }


  render() {

    return (
      <View>
      {
        !this.state.issubmit && (
          <View className="container" >
            <View className="item">
              <Text>我的地址</Text>
              <View onClick={this.chooseLocation}>{this.state.address}</View>
            </View>

            <View className="item">
              <Text>类型</Text>
              <View>
                <RadioGroup onChange={this.typechange}>
                  <Radio value="buy" checked="{this.state.type==='buy'?true:false}"></Radio>求购
      <Radio value="sell" checked="{this.state.type==='sell'?true:false}"></Radio>转让
    </RadioGroup>


              </View>
            </View>


            <View className="item">
              <Text>说明</Text>
              <View>
                <Input onInput={this.messageinput} placeholder="请填写您的具体需求"></Input>
              </View>
            </View>


            <View className="item">
              <Text>联系方式</Text>
              <View>
                <Input onInput={this.contactinput} placeholder="请填写您的联系方式"></Input>
              </View>
            </View>

            <View className="submit-btn" onClick={this.handleSubmit}>
              发布消息
</View>
          </View>
        )
      }

      {
        this.state.issubmit && <Feedback
        gohome={()=>this.goHomePage()}
        republish={()=>this.republish()}
        issuccess={this.state.issuccess}></Feedback>
      }
    </View>

    )
  }
}